package com.lu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.reggie.entity.Category;
import com.lu.reggie.entity.Dish;
import com.lu.reggie.entity.Setmeal;
import com.lu.reggie.exceptions.BusinessException;
import com.lu.reggie.mapper.CategoryMapper;
import com.lu.reggie.service.ICategoryService;
import com.lu.reggie.service.IDishService;
import com.lu.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements ICategoryService {
    @Autowired
    private IDishService iDishService;
    @Autowired
    private ISetmealService iSetmealService;
    /**
     * 自定义删除方法
     * @param id
     */
    @Override
    public void remove(Long id) {
        //在dish表中查询有无关联
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = iDishService.count(dishQueryWrapper);
        //判断
        if (dishCount > 0) {
            throw new BusinessException("已关联其他菜品!无法删除!");
        }
        //在setmeal表中查询有无关联
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setMealCount = iSetmealService.count(setmealQueryWrapper);
        if (setMealCount > 0) {
            throw new BusinessException("已关联套餐!无法删除!");
        }
        super.removeById(id);
    }
}
